﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Reflection;
using System.Collections;

namespace Enter.Common
{
   public class ReflectObjectHelper
    {

        /// <summary>
        /// 从DataTable中返回对象T列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="IType"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List<T> GetObjectListFromDataTableByType<T>(DataTable table) where T : class
        {
            List<T> result = new List<T>();

            Type _type = typeof(T);

            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow item in table.Rows)
                {
                    T obj = GetObjectFromDataRow<T>(item) as T;
                    if (obj != null)
                    {
                        result.Add(obj);
                    }

                }

            }

            return result;
        }

        /// <summary>
        ///  从DataReader中返回对象T列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="IType"></param>
        /// <param name="Reader"></param>
        /// <returns></returns>
        public static List<T> GetObjectListFromDataReaderByType<T>(IDataReader Reader) where T : class
        {
            List<T> result = new List<T>();

            Type _type = typeof(T);
            if (Reader != null)
            {
                while (Reader.Read())
                {
                    T obj = GetObjectFromDataRow(_type, Reader) as T;
                    if (obj != null)
                    {
                        result.Add(obj);
                    }
                }

            }
            return result;
        }

       /// <summary>
        ///从DataReader中返回对象
       /// </summary>
       /// <param name="type"></param>
       /// <param name="dr"></param>
       /// <returns></returns>
        public static object GetObjectFromDataRow(Type type, IDataReader dr)
        {

            object instance = Activator.CreateInstance(type);

            //获取属性信息 
            PropertyInfo[] myproperties = type.GetProperties();
            //从数据行中给对象赋值
            if (dr != null)
            {
                foreach (PropertyInfo item in myproperties)
                {
                    object dbValue = dr[item.Name.ToLower()];
                    SetpropertyInfoObject(item, ref instance, dbValue);
                }
                // return instance;
            }

            return instance;
        }

        /// <summary>
        /// 从表的单个行中返回对象
        /// </summary>
        /// <param name="type"></param>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static object GetObjectFromDataRow<T>(DataRow dr)
        {
            Type _type=typeof(T);

            object instance = Activator.CreateInstance(_type);        

            //获取属性信息 
            PropertyInfo[] myproperties = _type.GetProperties();
            //从数据行中给对象赋值
            if (dr != null)
            {
                foreach (PropertyInfo item in myproperties)
                {
                    object dbValue = dr[item.Name.ToLower()];
                    SetpropertyInfoObject(item, ref instance, dbValue);
                }
                // return instance;
            }

            return instance;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <param name="instance"></param>
        /// <param name="dbValue"></param>
        /// <returns></returns>
        private static object SetpropertyInfoObject(PropertyInfo propertyInfo, ref object instance, object dbValue)
        {
            Type enumType = null;
            if (propertyInfo.CanWrite)
            {
                object obj2 = null;
                if (Convert.IsDBNull(dbValue))
                {
                    propertyInfo.SetValue(instance, obj2, null);
                    return instance;
                }
                try
                {
                    enumType = propertyInfo.PropertyType;
                    if (enumType.BaseType.Equals(typeof(Enum)))
                    {
                        int num = Convert.ToInt32(dbValue);
                        propertyInfo.SetValue(instance, Enum.ToObject(enumType, num), null);
                        return instance;
                    }
                    propertyInfo.SetValue(instance, Convert.ChangeType(dbValue, enumType), null);
                }
                catch
                {
                    propertyInfo.SetValue(instance, Convert.ChangeType(dbValue, enumType), null);
                }
            }
            return instance;
        }
    }
}
